
#include "member_BTube.h"

using namespace BWE;

member_BTube::member_BTube(BTube* tube)
{
	boss = tube;
	radius = 0;
	height = 0;
	sides = 12;
	segments = 1;
	thickness = 1;
}
member_BTube::~member_BTube()
{

}

void member_BTube::prepare()
{
	if (sines.size() != sides)
	{
		sines.resize(sides);
		coses.resize(sides);
		float sr = PI2 / sides;
		for (int i = 0; i < sides; i++)
		{
			double angle = i * sr;
			coses[i] = cos(angle);
			sines[i] = sin(angle);
		}
	}
}

void member_BTube::rebuild()
{
	int flankNumVerts = sides * (segments + 1);
	int topNumVerts = 0;
	int bottomNumVerts = 0;
	int totalNumVerts = flankNumVerts + topNumVerts + bottomNumVerts;

	BVectorArray* vertices = boss->vertices();
	BVectorArray* normals = boss->normals();
	if (!vertices || vertices->size() != totalNumVerts)
	{
		vertices = new BVectorArray(totalNumVerts);
		normals = new BVectorArray(totalNumVerts);
		boss->setVertices(vertices);
		boss->setNormals(normals);
	}

	boss->clearPrimitives();

	float sz = height / segments;
	float stx = 1.0f / sides;
	float sty = 1.0f / segments;
	float sr = PI2 / sides;

	//make flank vertices.
	for (int r = 0; r < segments + 1; r++)
	{
		float z = sz * r;
		float ty = sty * r;
		int head = r * sides;
		for (int c = 0; c < sides; c++)
		{
			float nx = coses[c];
			float ny = sines[c];
			vertices->set(head + c, BVector(nx * radius, ny * radius, z));
			normals->set(head + c, BVector(nx, ny, 0));
		}
	}

}
